package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.BaseDao;
import com.delivery.www.dao.OrderDao;
import com.delivery.www.po.Dish;
import com.delivery.www.po.Order;
import com.delivery.www.service.DishService;
import com.delivery.www.service.ServiceImpl.DishServiceImpl;
import com.delivery.www.util.JdbcUtils;
import com.delivery.www.constant.Constants;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 12970
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public Order getOrder(int orderId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {orderId};
        String sql = "select user_id,store_id,dish_id,status,money from `order` where order_id = ?";
        Order order = null;
        if (connection != null) {
            order = BaseDao.getForOne(connection, Order.class, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return order;
    }

    @Override
    public int addOrder(int userId, int storeId, int dishId, int status, Float money, int couponId) {
        Connection connection = JdbcUtils.getConnection();
        int excute = 0;
        if (connection != null) {
            Object[] params = {userId, storeId, dishId, status, money, couponId};
            String sql = "insert into `order` (user_id,store_id,dish_id,status,money,coupon_id) values(?,?,?,?,?,?)";
            excute = BaseDao.executeUpdate(connection, sql, params);
            if (excute > 0) {
                Object[] objects1 = {storeId};
                String s1 = "update store set unprocess_order_num = unprocess_order_num+1 where store_id = ?";
                BaseDao.executeUpdate(connection, s1, objects1);

                Object[] objects2 = {money, userId};
                String s2 = "update user set balance = balance-? where user_id = ?";
                BaseDao.executeUpdate(connection, s2, objects2);
                if (couponId > 0) {
                    String s3 = "update have_coupon set status = 2 where coupon_id = ? and user_id = ?";
                    BaseDao.executeUpdate(connection, s3, couponId, userId);
                }
            }
            JdbcUtils.closeResource(connection, null, null);
        }
        return excute;
    }

    @Override
    public List<Order> getCompleteOrder(int storeId, int status) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {storeId, status};
        List<Order> orders = null;
        StringBuilder sql = new StringBuilder();
        sql.append("select u.user_name, o.order_id,o.dish_id,status,money,d.dish_name from `order` o ");
        sql.append("left join user u on o.user_id = u.user_id ");
        sql.append("left join dish d on d.dish_id = o.dish_id ");
        sql.append("where o.store_id = ? and o.status =? ");
        if (connection != null) {
            orders = BaseDao.getList(connection, Order.class, sql.toString(), params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return orders;
    }

    @Override
    public List<Order> getUnCompleteOrder(int storeId, int status) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {storeId, status};
        List<Order> orders = null;
        StringBuilder sql = new StringBuilder();
        sql.append("select u.user_name, o.order_id,o.dish_id,status,money,d.dish_name from `order` o ");
        sql.append("left join user u on o.user_id = u.user_id ");
        sql.append("left join dish d on d.dish_id = o.dish_id ");
        sql.append("where o.store_id = ? and o.status !=? ");
        if (connection != null) {
            orders = BaseDao.getList(connection, Order.class, sql.toString(), params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return orders;
    }

    @Override
    public int modifyStatus(int orderId, int status, int storeId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {status, orderId};
        String sql = "update `order` set status = ? where order_id = ?";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
            if (excute > 0 && status == Constants.TWO) {
                Order order = getOrder(orderId);
                Object[] objects1 = {storeId};
                String s1 = "update store set unprocess_order_num = unprocess_order_num-1,process_order_num=process_order_num+1 where store_id = ?";
                BaseDao.executeUpdate(connection, s1, objects1);
                Object[] objects2 = {order.getMoney(), storeId};
                String s2 = "update store set turnover = turnover + ? where store_id = ?";
                BaseDao.executeUpdate(connection, s2, objects2);
            }
        }
        JdbcUtils.closeResource(connection, null, null);
        return excute;
    }

    @Override
    public List<Order> getOwnOrder(int userId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {userId};
        List<Order> orders = null;
        StringBuilder sql = new StringBuilder();
        sql.append("select o.order_id,o.status,o.store_id,money,o.dish_id,s.store_name,d.dish_name from `order` o ");
        sql.append("left join store s on s.store_id = o.store_id ");
        sql.append("left join dish d on d.dish_id = o.dish_id ");
        sql.append("where o.user_id = ?");
        if (connection != null) {
            orders = BaseDao.getList(connection, Order.class, sql.toString(), params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return orders;
    }

    @Override
    public List<Integer> cartOrder(int userId, List<Integer> dishId) {
        Connection connection = JdbcUtils.getConnection();
        DishService dishService = new DishServiceImpl();
        List<Integer> integers = new ArrayList<>();
        if (connection != null) {
            for (Integer integer : dishId) {
                Dish dish = dishService.getOneDish(integer);
                int i = addOrder(userId, dish.getStoreId(), integer, 0, dish.getPrice(), 0);
                integers.add(i);
                if (i > 0) {
                    String sql = "delete from cart_dish where dish_id=?";
                    BaseDao.executeUpdate(connection, sql, integer);
                }
            }
            JdbcUtils.closeResource(connection, null, null);
        }
        return integers;
    }
}
